package com.vnp.cooking;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.vnp.cooking.UIcontrols.MyClickAbleImageView;
import com.vnp.cooking.adapter.CanhanListAdapter;
import com.vnp.cooking.adapter.FriendListAdapter;
import com.vnp.cooking.bean.ObjCookpad;
import com.vnp.cooking.httpclient.HttpClientHelper;
import com.vnp.cooking.httpclient.MyJsonHttpResponseHandler;
import com.vnp.cooking.utils.HelperListview;
import com.vnp.cooking.utils.PreferenceHelper;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.YuvImage;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class XemInfoUserActivity extends Activity implements OnClickListener{
	
	ImageButton btnBack;
	ArrayList<ObjCookpad> objCookpads = new ArrayList<ObjCookpad>();
	ObjCookpad objCookpad = new ObjCookpad();
	// load more activity
	ProgressBar prbLoadActivity;
	ProgressBar progressBarLoadmore;
	Button btnLoadMore;
	View  footerMain;
	int mPage = 0;
	FriendListAdapter adapter;
	Handler mHandler = new Handler();
	
	// ten
	TextView txtFriendName, txtYeuthich, txtDadang;
	ListView listActivity;
	PreferenceHelper preferenceHelper;
	
	Runnable r = new Runnable() {
		
		@Override
		public void run() {
			getActivityUser();
		}
	};
	int yeuthich, dadang;
	int isTurn = 0;
	MyClickAbleImageView btnYeuthich, btnDadang;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_trang_banbe);
		preferenceHelper = new PreferenceHelper(this);
		objCookpad = getIntent().getParcelableExtra("objCookpad");
		
		btnBack = (ImageButton) findViewById(R.id.btn_back);
		txtFriendName = (TextView) findViewById(R.id.txt_friend_name);
		listActivity = (ListView) findViewById(R.id.listActivityUser);
		prbLoadActivity = (ProgressBar) findViewById(R.id.prgGetActivity);
		
		btnYeuthich = (MyClickAbleImageView) findViewById(R.id.btn_dathich);
		btnDadang = (MyClickAbleImageView) findViewById(R.id.btn_dadang);
		
		txtYeuthich = (TextView) findViewById(R.id.txt_yeuthich);
		txtDadang = (TextView) findViewById(R.id.txt_dadang);
		
		getInfoFriend();
		
		mHandler.post(r);
		adapter = new FriendListAdapter(XemInfoUserActivity.this, objCookpads);
		listActivity.setAdapter(adapter);
		HelperListview.getListViewSize(listActivity);
		init();
		
	}
	
	void init(){
		btnBack.setOnClickListener(this);
		txtFriendName.setText(objCookpad.getNamePersonCookpad());
		addFooterView();
		btnYeuthich.setOnClickListener(this);
		btnDadang.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			closeActivity();
			break;
		case R.id.btn_dathich:
			Intent c = new Intent(this, CongthucDaThichActivity.class);
			c.putExtra("userID", objCookpad.getIdNamePerson());
			startActivity(c);
			overridePendingTransition(R.anim.open_next, R.anim.close_main);
			break;
		case R.id.btn_dadang:
			Intent a = new Intent(this, CongthucDadangActivity.class);
			a.putExtra("userID", objCookpad.getIdNamePerson());
			startActivity(a);
			overridePendingTransition(R.anim.open_next, R.anim.close_main);
			break;
		default:
			break;
		}
	}
	
	
	void getActivityUser() {
		HttpClientHelper httpClientHelper = new HttpClientHelper(new MyJsonHttpResponseHandler(){

			@Override
			public void onFailure(Throwable error) {
				super.onFailure(error);
				prbLoadActivity.setVisibility(View.GONE);
			}

			@Override
			public void onSuccess(JSONObject re) {
				prbLoadActivity.setVisibility(View.GONE);
				Log.i("DATA", "Nhay vao sucess");
				try {
					int status = re.getInt("status");
					if (status == 200) {
						JSONArray data = re.getJSONArray("data");
						isTurn = isTurn + 1;
						Log.i("DATA", "ACIVITY: "+data.toString());
						adapter.clear();
						for (int i = 0; i < data.length(); i++) {
							JSONObject jo = data.getJSONObject(i);
							JSONObject tacgia = jo.getJSONObject("author");
							
							ObjCookpad objCookpad = new ObjCookpad();
							objCookpad.setId(jo.getString("id"));
							objCookpad.setIdNamePerson(tacgia.getString("id"));
							objCookpad.setNamePersonCookpad(tacgia.getString("name"));
							objCookpad.setDescriptionCookpad(jo.getString("desc"));	
							objCookpad.setNameCookpad(jo.getString("name"));
							
							JSONObject images = jo.getJSONObject("image");
							objCookpad.setUrlImageCookpad(images.getString("cover"));
							
							objCookpad.setThoigianNauan(jo.getString("time"));
							objCookpad.setCreatedDateCookpad(jo.getString("created"));
							objCookpad.setAction(jo.getString("action"));
							objCookpad.setViewedCookpad(jo.getInt("view_count"));
							objCookpad.setLikeCookpad(jo.getInt("like_count"));
							
							
							objCookpads.add(objCookpad);
							
						} 
						adapter.notifyDataSetChanged();
						if (objCookpads.size() > 10) {
							footerMain.setVisibility(View.VISIBLE);
						}
					} else {
						
					}
					
					
				} catch (JSONException e) {
					e.printStackTrace();
					prbLoadActivity.setVisibility(View.GONE);
				}
			}
		});
		httpClientHelper.getInfoActivity(objCookpad.getIdNamePerson(), mPage);
			
		}
	
	void getInfoFriend() {
		HttpClientHelper httpClientHelper = new HttpClientHelper(new MyJsonHttpResponseHandler(){

			@Override
			public void onFailure(Throwable error) {
				super.onFailure(error);
			}

			@Override
			public void onSuccess(JSONObject re) {
				try {
					int status = re.getInt("status");
					if (status == 200) {
						JSONObject data = re.getJSONObject("data");
						
						Log.i("DATA", "Thong tin USER: "+data.toString());
						
						String name = data.getString("name");
						String levelName = data.getString("level_name");
						String about = data.getString("about");
						int cityID = data.getInt("city_id");
						String avatar = data.getString("avatar");
						yeuthich = data.getInt("total_favourite");
						dadang = data.getInt("total_created");
						preferenceHelper.setFriendURLAvatar(avatar);
						preferenceHelper.setFriendName(name);
						
						Log.i("DATA", "AVATAR :"+avatar);
						
						adapter.notifyDataSetChanged();
						txtYeuthich.setText("Yêu thích ("+""+yeuthich+")");
						txtDadang.setText("Đã đăng ("+""+dadang+")");
					} else {
						
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
		});
		httpClientHelper.getInfoUser(objCookpad.getIdNamePerson());
	}
	
	void addFooterView() {
		View loadMoreView = ((LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.footer_loadmore, null, false);
		footerMain = (View) loadMoreView.findViewById(R.id.footer_main);
		footerMain.setVisibility(View.GONE);
		listActivity.addFooterView(loadMoreView);
		btnLoadMore = (Button) loadMoreView.findViewById(R.id.btn_load_more);
		progressBarLoadmore = (ProgressBar) loadMoreView.findViewById(R.id.progressBar1);
		btnLoadMore.setOnClickListener(this);
	}
	void closeActivity(){
		finish();
		overridePendingTransition(R.anim.open_main, R.anim.close_next);
	}


	@Override
	public void onBackPressed() {
		closeActivity();
	}

}
